package com.huawei.demo;

import com.huaweicloud.sdk.codeartsbuild.v3.CodeArtsBuildClient;
import com.huaweicloud.sdk.codeartsbuild.v3.model.RunJobRequest;
import com.huaweicloud.sdk.codeartsbuild.v3.model.RunJobRequestBody;
import com.huaweicloud.sdk.codeartsbuild.v3.model.RunJobResponse;
import com.huaweicloud.sdk.codeartsbuild.v3.model.ShowJobListByProjectIdRequest;
import com.huaweicloud.sdk.codeartsbuild.v3.model.ShowJobListByProjectIdResponse;
import com.huaweicloud.sdk.codeartsbuild.v3.model.ShowJobStatusRequest;
import com.huaweicloud.sdk.codeartsbuild.v3.model.ShowJobStatusResponse;
import com.huaweicloud.sdk.codeartsbuild.v3.model.ShowListHistoryRequest;
import com.huaweicloud.sdk.codeartsbuild.v3.model.ShowListHistoryResponse;
import com.huaweicloud.sdk.codeartsbuild.v3.region.CodeArtsBuildRegion;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.core.http.HttpConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CodeArtsBuildBuildTaskDemo {
    private static final Logger LOGGER = LoggerFactory.getLogger(CodeArtsBuildBuildTaskDemo.class.getName());

    public static void main(String[] args) {
        String ak = "<YOUR AK>";
        String sk = "<YOUR SK>";

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);
        // 创建client实例
        CodeArtsBuildClient client = CodeArtsBuildClient.newBuilder()
                .withCredential(auth).withRegion(CodeArtsBuildRegion.CN_NORTH_4)
                .withHttpConfig(new HttpConfig().withIgnoreSSLVerification(true).withTimeout(60000)).build();
        // 查看项目下用户的构建任务列表
        ShowJobListByProjectIdRequest request = new ShowJobListByProjectIdRequest();
        request.withProjectId("<YOUR PROJECT ID>");
        request.withPageIndex(0);
        request.withPageSize(1);
        try {
            ShowJobListByProjectIdResponse response = client.showJobListByProjectId(request);
            LOGGER.info(response.toString());
            if (response.getJobs() == null || response.getJobs().isEmpty()) {
                LOGGER.info("There is no task under the project.");
                return ;
            }
            String jobId = response.getJobs().get(0).getId();
            showListHistory(jobId, client);
            runJob(jobId, client);
            showJobStatus(jobId, client);
        } catch (ConnectionException e) {
            LOGGER.error(e.toString());
        } catch (RequestTimeoutException e) {
            LOGGER.error(e.toString());
        } catch (ServiceResponseException e) {
            LOGGER.error(e.toString());
            LOGGER.error(String.valueOf(e.getHttpStatusCode()));
            LOGGER.error(e.getErrorCode());
            LOGGER.error(e.getErrorMsg());
        }
    }

    public static void showListHistory(String jobId, CodeArtsBuildClient client) {
        // 查看构建任务的构建历史列表
        ShowListHistoryRequest slhRequest = new ShowListHistoryRequest();
        slhRequest.withInterval(1);
        slhRequest.withJobId(jobId);
        slhRequest.withOffset(1);
        slhRequest.withLimit(1);
        ShowListHistoryResponse slhResponse = client.showListHistory(slhRequest);
        LOGGER.info(slhResponse.toString());
    }

    public static void runJob(String jobId, CodeArtsBuildClient client) {
        // 执行构建任务
        RunJobRequest rjRequest = new RunJobRequest();
        RunJobRequestBody body = new RunJobRequestBody();
        body.withJobId(jobId);
        rjRequest.withBody(body);
        RunJobResponse RJResponse = client.runJob(rjRequest);
        LOGGER.info(RJResponse.toString());
    }

    public static void showJobStatus(String jobId, CodeArtsBuildClient client) {
        // 查看任务运行状态
        ShowJobStatusRequest sjsRequest = new ShowJobStatusRequest();
        sjsRequest.withJobId(jobId);
        ShowJobStatusResponse sjsResponse = client.showJobStatus(sjsRequest);
        LOGGER.info(sjsResponse.toString());
    }
}
